import sklearn.datasets as datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import KNeighborsRegressor

# 抓取数据，当做样本
# 鸢尾花样本
iris = datasets.load_iris()
print(iris)
# 样本数据
x_train = iris.data[::2]
print(x_train)

y_train = iris.target[::2]
print(y_train)

# 测试数据集
x_test = iris.data[1::2]
y_test = iris.target[1::2]

knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(x_train, y_train)
result = knn.predict(x_test)
print(result)
score = knn.score(x_test, y_test)
print(score)

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

cmap = ListedColormap(["#FF0000", "#00FF00", "#0000FF"])

plt.scatter(iris.data[:, 2], iris.data[:, 3], c=iris.target, cmap=cmap)
# plt.plot(x[:34])
